import java.util.Stack;
/**
 * 算法匹配：括号匹配
 * Created by zhaoxin on 2018/7/15.
 */
public class Solution {

    public boolean isVaild(String str){
        // 创建栈
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '{' || c == '[' || c == '('){
                stack.push(c);
            }else {
                if (stack.empty()){
                    return false;
                }
                // 将栈顶元素取出，与当前循环元素比较如果匹配则再一次循环，如果不匹配则返回false
                char popElement = stack.pop();
                if (c == '}' && popElement != '{'){
                    return false;
                }else if (c == ']' && popElement != '['){
                    return false;
                }else if (c == ')' && popElement != '('){
                    return false;
                }
            }
        }

        return stack.isEmpty();
    }

    public static void main(String[] args) {
        Solution solution = new Solution();

        boolean flag = solution.isVaild("]");
        System.out.println(flag);
    }

}
